package com.citywithincity.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class HttpsUtil {
	private static final SSLContext sc;
	private static final HostnameVerifier HOSTNAME_VERIFIER = new TrustAnyHostnameVerifier();
	static{
		try {
			sc = SSLContext.getInstance("SSL", "SunJSSE");
			sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new SecureRandom());
		} catch (Exception e){
			throw new RuntimeException(e);
		}
		
	}
	
	
	private static class TrustAnyHostnameVerifier implements HostnameVerifier {
		public boolean verify(String hostname, SSLSession session) {
			return true;
		}
	}

	private static class TrustAnyTrustManager implements X509TrustManager {
		public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
		}

		public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
		}

		public X509Certificate[] getAcceptedIssuers() {
			return new X509Certificate[0];
		}
	}
	
	public static void setConnections(HttpsURLConnection httpConnection){
		httpConnection.setSSLSocketFactory(sc.getSocketFactory());
		httpConnection.setHostnameVerifier(HOSTNAME_VERIFIER);
	}

	public static String rawPost(String strURL, String requesstMessage) throws IOException {
		URL url = null;
		HttpsURLConnection httpConnection = null;
		InputStream in = null;
		OutputStream out = null;
		BufferedReader br = null;
		try {
			url = new URL(strURL);
			httpConnection = (HttpsURLConnection) url.openConnection();
			setConnections(httpConnection);

			httpConnection.setRequestMethod("POST");
			httpConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
			httpConnection.setRequestProperty("Cache-Control", "no-cache");
			
			
			httpConnection.setDoInput(true);
			httpConnection.setDoOutput(true);
			httpConnection.setConnectTimeout(5000);
			httpConnection.setReadTimeout(5000);

			out = httpConnection.getOutputStream();
			out.write(requesstMessage.getBytes("UTF-8"));
			out.flush();

			in = httpConnection.getInputStream();
			br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
			String strRead = "";
			StringBuilder sbReturn = new StringBuilder();
			while ((strRead = br.readLine()) != null) {
				sbReturn.append(strRead);
			}
			return sbReturn.toString();
		} finally {
			IoUtil.close(out);
			IoUtil.close(in);
			IoUtil.close(br);
			if (httpConnection != null) {
				httpConnection.disconnect();
			}
		}

	}

}
